import urllib.request
import urllib.parse
from lxml import etree


# https://sc.chinaz.com/tupian/shanggan.html

# https://sc.chinaz.com/tupian/shanggan_2.html
def creat_request(page):
    if(page==1):
        url="https://sc.chinaz.com/tupian/shanggan.html"
    else:
        url="https://sc.chinaz.com/tupian/shanggan_"+str(page)+".html"


    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
        ,
        "Cookie": "BAIDUID=61DE5B3752CCC2FA8652E9AB276B49F4:FG=1; BIDUPSID=61DE5B3752CCC2FA8652E9AB276B49F4; PSTM=1716729875; BDUSS=1ZaYnFFVlFMNElHZm5yaW0wOX5LenNKR0NwVmNJYWJIVkd2eHpqa0p-cGpJcEZtSVFBQUFBJCQAAAAAAQAAAAEAAAASQI9ax6vO918AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGOVaWZjlWlmd; BDUSS_BFESS=1ZaYnFFVlFMNElHZm5yaW0wOX5LenNKR0NwVmNJYWJIVkd2eHpqa0p-cGpJcEZtSVFBQUFBJCQAAAAAAQAAAAEAAAASQI9ax6vO918AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGOVaWZjlWlmd; H_WISE_SIDS=60326_60337; H_WISE_SIDS_BFESS=60326_60337; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_PSSID=60337; delPer=0; BAIDUID_BFESS=61DE5B3752CCC2FA8652E9AB276B49F4:FG=1; BA_HECTOR=0l2l2k8gah2h0g0h0g852g018del7t1j7ipam1u; PSINO=6; ZFY=kNVDyFZpg9GOCSzPtMRjRv1xk4WgLZlPNbsXn8zoQ:Aw:C; BDRCVFR[V3EOV_cRy1C]=mk3SLVN4HKm"
    }

    request = urllib.request.Request(url=url, headers=headers)
    return request


def get_content(request):
    response = urllib.request.urlopen(request)
    content = response.read().decode('utf-8')
    return content


def down_load(content):
    tree = etree.HTML(content)
    name_list=tree.xpath('//div[@class="container"]//img/@alt')
    # for name in name_list:
    #     print(name)

    # 图片的网站   懒加载   src变为src2
    src_list=tree.xpath('//img/@data-original')
    for i in range(len(name_list)):
        name=name_list[i]
        src=src_list[i]
        src=src.replace('_s','')
        url='https:'+src
        # filename = ”下载路径“ +   ”下载保存的名字”+后缀名
        urllib.request.urlretrieve(url=url,filename='./picture/'+name+'.jpg')


if __name__ == '__main__':
    start_page = int(input("起始页面"))
    end_page=int(input("结束页面"))

    for page in range(start_page,end_page+1):
        # 请求对象定制
     request =  creat_request(page)
         # 获取网页源码
     content=get_content(request)
        # 下载
     down_load(content)